﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;

namespace App.Utility
{
    /// <summary>
    /// Written By: Charles Cook
    /// 
    /// http://www.charlesrcook.com/archive/2008/07/23/c-extension-methods-for-asp.net-query-string-operations.aspx
    /// </summary>
    public static class QueryStringExtensions
    {

        public static string WriteLocalPathWithQuery(
            this NameValueCollection collection,
            Uri Url)
        {
            if (collection.Count == 0)
                return Url.LocalPath;

            StringBuilder sb = new StringBuilder(Url.LocalPath);
            sb.Append("?");

            for (int i = 0; i < collection.Keys.Count; i++)
            {
                if (i != 0)
                    sb.Append("&");
                sb.Append(
                    String.Format("{0}={1}",
                    collection.Keys[i], collection[i])
                    );
            }
            return sb.ToString();
        }

        public static NameValueCollection RemoveItem(this NameValueCollection collection, string field)
        {
            for (int i = 0; i < collection.Keys.Count; i++)
            {
                if (collection.Keys[i].Equals(field, StringComparison.OrdinalIgnoreCase))
                {
                    collection.Remove(collection.Keys[i]);
                    return collection;
                }
            }
            return collection;
        }

        public static string WriteQueryToInputFields(this NameValueCollection collection)
        {
            string hiddenTemplate = @"<input type=""hidden"" name=""{0}"" value=""{1}"" ID=""{0}"">";
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < collection.Keys.Count; i++)
            {
                sb.Append(String.Format(hiddenTemplate, collection.Keys[i], collection[i]));
            }
            return sb.ToString();
        }

        public static NameValueCollection NegateField(this NameValueCollection collection, string Key)
        {
            bool worker = false;
            if (!String.IsNullOrEmpty(collection[Key]))
            {
                Boolean.TryParse(collection[Key], out worker);
                worker = !worker;
            }
            collection[Key] = Convert.ToString(worker);
            return collection;
        }

        public static NameValueCollection ChangeField(this NameValueCollection collection,
            string Key, string Value)
        {
            return ChangeField(collection, Key, Value, true);
        }

        public static NameValueCollection ChangeField(this NameValueCollection collection,
            string Key, string Value, bool Allow)
        {
            if (Allow)
            {
                if (!String.IsNullOrEmpty(collection[Key]))
                    collection[Key] = Value;
                else
                    collection.Add(Key, Value);
            }
            else //remove the value all together
            {
                if (!String.IsNullOrEmpty(collection[Key]))
                    collection.Remove(Key);
            }
            return collection;
        }

        public static NameValueCollection Duplicate(this NameValueCollection source)
        {
            NameValueCollection collection = new NameValueCollection();
            foreach (string key in source)
                collection.Add(key, source[key]);
            return collection;
        }
    }
}
